package com.ip.student.service;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ip.student.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ip.student.mapper.StudentPunishmentMapper;
import com.ip.student.models.service.IStudentPunishmentService;
import com.ip.student.models.bo.StudentPunishmentSaveBO;
import com.ip.student.models.dto.StudentPunishmentDTO;

/**
 * 学生处分信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-17 10:16:07
 */
@DubboService
public class StudentPunishmentServiceImpl extends ServiceImpl<StudentPunishmentMapper, StudentPunishmentDTO> implements IStudentPunishmentService{

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(StudentPunishmentSaveBO saveBO) {
        StudentPunishmentDTO dto = new StudentPunishmentDTO();
        BeanUtils.copyProperties(saveBO,dto);
        
        // 如果前端传回的ID为null或0，则使用雪花算法生成ID，但限制在Integer范围内
        if (saveBO.getPunishId() == null || saveBO.getPunishId() == 0) {
            Long id = ymlConfig.getPkId();
            // 确保生成的ID为正数且在Integer范围内
            if (id < 0) {
                id = Math.abs(id);
            }
            // 确保ID在Integer范围内
            if (id > Integer.MAX_VALUE) {
                id = id % Integer.MAX_VALUE;
            }
            // 确保ID不为0
            if (id == 0) {
                id = 1L;
            }
            dto.setPunishId(id.intValue());
        } 
        return this.insert(dto);
    }
}